﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.Animations;
using System.IO;
using System.Reflection;
using Silvermoon.Images;
using Silvermoon.Visuals;
using Silvermoon.OpenGL;
using DialogResult=Silvermoon.Controls.Classes.DialogResult;
using PictureBox=Silvermoon.Controls.PictureBox;

namespace FileExplorer
{

    public class MainMenu : Window
    {
        private Calculator calculator;
        private AddWeightWindow AddWeightWindow;
        private AddFoodWindow AddFoodWindow;
        private AddExerciseWindow AddExerciseWindow;
        private ViewWeightWindow ViewWeightWindow;
        private AddMoneyWindow AddMoneyWindow;
        private AddPetrolWindow AddPetrolWindow;
        private PictureBox box;
        private LensView view;
        private ExplorerWindow explorer;
        private StartMenuItem dateItem;
        int seconds;

        public MainMenu()
            : base()
        {
            int height = StartMenuItem.ItemHeight;
            view = new LensView { Margin = new Margin(0), TextColor = Color.White, LensHeight = 160, ItemHeight = height, Locked = false, KeepLensCentered = false };
            TextColor = Color.White;
            LoadMenu();
        }

        private void LoadMenu()
        {
            // this windows does not need a toolbar:
            ShowToolbar = false;

            Texture b1 = new Texture();
            Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileLife.Images.RegEdit.png");
            b1.LoadFromStream(str);

            // Get the background image:
            box = new PictureBox { Margin = new Margin(0) };
            box.Image = new Texture();
            SetBackground("Space.jpg");
            Controls.Add(box);


            Controls.Add(view);
            StartMenuItem smi = new StartMenuItem("Mobile Life"
                                                  , new StartMenuDetailChunk { Title = "Mobile Life", Details = "V" + this.GetType().Assembly.GetName().Version.ToString() + " Melectric 2009" }
                                                  , new StartMenuDetailChunk {Title = "Debug Mode", Details = "Press to switch"}
                );
            smi.Chunks[1].Click += (s, e) => DebugMode();
            view.Items.Add(smi);


            smi = new StartMenuItem("Money"
                , new StartMenuDetailChunk
                {
                    Title = "Money - Add",
                    Details = "Add Record to log"
                });

            smi.Chunks[0].Click += (s, e) => AddMoney();
            view.Items.Add(smi);

            smi = new StartMenuItem("Petrol"
                , new StartMenuDetailChunk
                {
                    Title = "Petrol - Add",
                    Details = "Add Record to log"
                });

            smi.Chunks[0].Click += (s, e) => AddPetrol();
            view.Items.Add(smi);

            smi = new StartMenuItem("Weight"
                                    , new StartMenuDetailChunk { Title = "Weight - Add", Details = "Add Record to log" }
                                    , new StartMenuDetailChunk { Title = "Weight - View", Details = "View History" }
                
                );
            smi.Chunks[0].Click += (s, e) => AddWeight();
            smi.Chunks[1].Click += (s, e) => ViewWeight();
            view.Items.Add(smi);

            smi = new StartMenuItem("Food"
                        , new StartMenuDetailChunk { Title = "Food - Add", Details = "Add Record to log" }

    );
            smi.Chunks[0].Click += (s, e) => AddFood();
            view.Items.Add(smi);

            smi = new StartMenuItem("Exercise"
            , new StartMenuDetailChunk
            {
                Title = "Exercise - Add", 
                Details = "Add Record to log" 
            });

            smi.Chunks[0].Click += (s, e) => AddExercise();
            view.Items.Add(smi);


            smi = new StartMenuItem("Exit", "Exits the application.", "MobileLife.Images.Exit.png");
            smi.Chunks[0].Click += (s, e) => ExitApp();
            view.Items.Add(smi);


            // set the lens and the offset to a nice position:
            view.TopOffset = 0;
            view.LensOffset = 0;
        }

        private void AddExercise()
        {

            if (AddExerciseWindow == null)
            {
                AddExerciseWindow = new AddExerciseWindow();
                AddExerciseWindow.Hiding += OnExplorerHiding;
                AddExerciseWindow.Shown += (s, e) => { this.Visible = false; };
            }
            AddExerciseWindow.Show(TransitionMode.ZoomIn);
            AddExerciseWindow.Focus();
        }


        /// <summary>
        /// Sets the background image.
        /// </summary>
        /// <param name="name">The name of the image to get from resource.</param>
        private void SetBackground(string name)
        {
            string resourcePath = string.Format("MobileLife.Images.{0}", name);
            Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(resourcePath);
            if (stream == null) throw new SystemException("Resource not found: " + resourcePath);
            box.Image.LoadFromStream(stream);

        }

        /// <summary>
        /// Method when an app closes
        /// </summary>
        void OnExplorerHiding(object sender, EventArgs e)
        {
            this.Visible = true;
            this.SendToBack();
            PreLoad();
        }

        /// <summary>
        /// Open Add Wieght
        /// </summary>
        private void AddWeight()
        {
            if (AddWeightWindow == null)
            {
                AddWeightWindow = new AddWeightWindow();
                AddWeightWindow.Hiding += OnExplorerHiding;
                AddWeightWindow.Shown += (s, e) => { this.Visible = false; };
            }
            AddWeightWindow.Show(TransitionMode.ZoomIn);
            AddWeightWindow.Focus();
        }

        /// <summary>
        /// Open View Wieght
        /// </summary>
        private void ViewWeight()
        {
            if (ViewWeightWindow == null)
            {
                ViewWeightWindow = new ViewWeightWindow();
                ViewWeightWindow.Hiding += OnExplorerHiding;
                ViewWeightWindow.Shown += (s, e) => { this.Visible = false; };
            }
            ViewWeightWindow.Show(TransitionMode.ZoomIn);
            ViewWeightWindow.Focus();
        }

        /// <summary>
        /// Open Add Food
        /// </summary>
        private void AddFood()
        {
            if (AddFoodWindow == null)
            {
                AddFoodWindow = new AddFoodWindow();
                AddFoodWindow.Hiding += OnExplorerHiding;
                AddFoodWindow.Shown += (s, e) => { this.Visible = false; };
            }
            AddFoodWindow.Show(TransitionMode.ZoomIn);
            AddFoodWindow.Focus();
        }

        /// <summary>
        /// Open Add Money
        /// </summary>
        private void AddMoney()
        {
            if (AddMoneyWindow == null)
            {
                AddMoneyWindow = new AddMoneyWindow();
                AddMoneyWindow.Hiding += OnExplorerHiding;
                AddMoneyWindow.Shown += (s, e) => { this.Visible = false; };
            }
            AddMoneyWindow.Show(TransitionMode.ZoomIn);
            AddMoneyWindow.Focus();
        }


        /// <summary>
        /// Open Add Petrol
        /// </summary>
        private void AddPetrol()
        {
            if (AddPetrolWindow == null)
            {
                AddPetrolWindow = new AddPetrolWindow();
                AddPetrolWindow.Hiding += OnExplorerHiding;
                AddPetrolWindow.Shown += (s, e) => { this.Visible = false; };
            }
            AddPetrolWindow.Show(TransitionMode.ZoomIn);
            AddPetrolWindow.Focus();
        }

        /// <summary>
        /// Open Add Wieght
        /// </summary>
        private void DebugMode()
        {
            StreamReader sr;
            string messagetext = "";
            try
            {
                sr = new StreamReader("debug.txt");
            }
            catch (Exception)
            {
                StreamWriter sw = new StreamWriter("debug.txt");
                sw.WriteLine("true");
                sw.Close();
                
                sr = new StreamReader("debug.txt");
            }
            if(sr.ReadLine()== "true")
            {
                sr.Close();
                StreamWriter sw = new StreamWriter("debug.txt");
                sw.WriteLine("false");
                sw.Close();
                messagetext = "OFF";
            }
            else
            {
                sr.Close();
                StreamWriter sw = new StreamWriter("debug.txt");
                sw.WriteLine("true");
                sw.Close();
                messagetext = "ON";
            }
            ConfirmDialog dlg = new ConfirmDialog("Debug Mode", messagetext, true);

            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
            {

            });

        }

        /// <summary>
        /// To ensure to exit this application we show a dialog to confirm:
        /// </summary>
        private void ExitApp()
        {
            ConfirmDialog dlg = new ConfirmDialog("Exit Application", "Do you really want to quit?");
            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
                {
                    if (e.Result == DialogResult.OK) Screen.Close();
                });
        }

        public override void Focus()
        {
            view.Focus();
        }

    }
}
